Spot-On
spot-on-utilities.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_utilities_h_
29 #define _spoton_utilities_h_
30 
31 #include <QDesktopWidget>
32 #include <QScreen>
33 #include <QWidget>
34 
36 {
37  public:
38  static void centerWidget(QWidget *child, QWidget *parent)
39  {
40  if(!child || !parent)
41  return;
42 
43  /*
44  ** From QDialog.
45  */
46 
47 #ifdef Q_WS_X11
48  if(X11->isSupportedByWM(ATOM(_NET_WM_FULL_PLACEMENT)))
49  return;
50 #endif
51 
52 #ifdef Q_OS_SYMBIAN
53  /*
54  ** Perhaps implement symbianAdjustedPosition().
55  */
56 #endif
57 
58  QPoint p(0, 0);
59  int extrah = 0, extraw = 0, scrn = 0;
60 
61  if(parent)
62  parent = parent->window();
63 
64  QRect desk;
65 
66  if(parent)
67  scrn = QApplication::desktop()->screenNumber(parent);
68 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
69  else if(QApplication::desktop()->isVirtualDesktop())
70  scrn = QApplication::desktop()->screenNumber(QCursor::pos());
71 #endif
72  else
73  scrn = QApplication::desktop()->screenNumber(child);
74 
75 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
76  desk = QApplication::desktop()->availableGeometry(scrn);
77 #else
78  desk = QGuiApplication::screens().value(scrn) ?
79  QGuiApplication::screens().value(scrn)->geometry() : QRect();
80 #endif
81 
82  QWidgetList list = QApplication::topLevelWidgets();
83 
84  for(int i = 0; (extrah == 0 || extraw == 0) && i < list.size(); ++i)
85  {
86  QWidget *current = list.at(i);
87 
88  if(current && current->isVisible())
89  {
90  int frameh = current->geometry().y() - current->y();
91  int framew = current->geometry().x() - current->x();
92 
93  extrah = qMax(extrah, frameh);
94  extraw = qMax(extraw, framew);
95  }
96  }
97 
98  if(extrah == 0 || extrah >= 40 || extraw == 0 || extraw >= 10)
99  {
100  extrah = 40;
101  extraw = 10;
102  }
103 
104  if(parent)
105  {
106  QPoint pp = parent->mapToGlobal(QPoint(0,0));
107 
108  p = QPoint(pp.x() + parent->width() / 2,
109  pp.y() + parent->height() / 2);
110  }
111  else
112  p = QPoint(desk.x() + desk.width() / 2, desk.y() + desk.height() / 2);
113 
114  p = QPoint(p.x() - child->width() / 2 - extraw,
115  p.y() - child->height() / 2 - extrah);
116 
117  if(p.x() + extraw + child->width() > desk.x() + desk.width())
118  p.setX(desk.x() + desk.width() - child->width() - extraw);
119 
120  if(p.x() < desk.x())
121  p.setX(desk.x());
122 
123  if(p.y() + extrah + child->height() > desk.y() + desk.height())
124  p.setY(desk.y() + desk.height() - child->height() - extrah);
125 
126  if(p.y() < desk.y())
127  p.setY(desk.y());
128 
129  child->move(p);
130  }
131 
132  private:
133  spoton_utilities(void);
134 };
135 
136 #endif
spoton_utilities
Definition: spot-on-utilities.h:35